package com.yeyks.util.arithmetic;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.Random;

/**
 * @author fuchangshuai
 * @date 2019/5/5 0005
 */
public class Lottery {

    /**
     * 获取随机金额（分）
     *
     * @param probabilitys
     * @return
     */
    public static Integer lottery(List<Probability> probabilitys) {
        //总的概率区间
        Integer total = 0;
        //存储每个奖品新的概率区间
        List<Integer> newProbabilitys = Lists.newArrayList();
        newProbabilitys.add(0);
        //遍历每个奖品，设置概率区间，总的概率区间为每个概率区间的总和
        for (Probability probability : probabilitys) {
            total += probability.getProbability();
            newProbabilitys.add(total);
        }
        //获取总的概率区间中的随机数
        Random random = new Random();
        Integer randomPro = random.nextInt(total);
        //判断取到的随机数在哪个奖品的概率区间中
        for (int i = 0; i < newProbabilitys.size(); i++) {
            if (randomPro >= newProbabilitys.get(i)
                    && randomPro < newProbabilitys.get(i + 1)) {
                return getData(probabilitys.get(i), random);
            }
        }
        return 3000;
    }

    /**
     * 获取随机数
     * 30-50元       60%
     * 60-100元      37%
     * 110-190元     2.9%
     * 200元（免单券）0.1%
     * 所有优惠券金额为整10倍数
     *
     * @param probability
     * @param random
     * @return
     */
    private static Integer getData(Probability probability, Random random) {
        int minPrice = probability.getMinPrice();
        int maxPrice = probability.getMaxPrice();
        int randomData = random.nextInt(maxPrice - minPrice == 0 ? 1 : maxPrice - minPrice);
        int result = minPrice + randomData;
        return ((result + 9) / 10) * 10 * 100;
    }

    public static void main(String[] args) {
        List<Probability> probabilities = Lists.newArrayList();
        probabilities.add(new Probability(30, 50, 600));
        probabilities.add(new Probability(60, 100, 370));
        probabilities.add(new Probability(110, 190, 29));
        probabilities.add(new Probability(200, 200, 1));
        for (int i = 0; i < 10000; i++) {
            System.out.println(lottery(probabilities));
        }
    }
}
